<template>
  <div class="app-container">
    <eHeader :param-bean="paramBean" :sup_this="sup_this" />
    <eForm ref="form" :sup_this="sup_this" />
    <!--表格渲染-->
    <DataGridTable
      v-loading="loading"
      ref="mainTable"
      :sup_this="sup_this"
      :data="data.rtnList"
      :total="total">
      <el-table-column prop="flowKey" label="流程Key" width="120px" />
      <el-table-column prop="flowName" label="流程名称">
        <template slot-scope="scope">
          <span v-if="scope.row.flowState === '0'">{{ scope.row.flowName }}</span>
          <a v-else href="#" title="查看流程图" @click="onPreview(scope.row.flowKey)">{{ scope.row.flowName }}</a>
        </template>
      </el-table-column>
      <el-table-column prop="flowVersion" label="版本" width="60px" />
      <el-table-column label="状态" align="center" width="80px">
        <template slot-scope="scope">
          <div v-for="item in dicts.flow_state" :key="item.id">
            <span v-if="scope.row.flowState.toString() === item.dictCode" :type="''">{{ item.dictName }}</span>
          </div>
        </template>
      </el-table-column>
      <el-table-column prop="lastUpdateDate" label="更新日期" width="200px">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.lastUpdateDate) }}</span>
        </template>
      </el-table-column>
      <el-table-column v-if="checkPermission(['SYS_ADMIN','COMMON_ADMIN','WORKFLOW_DEF_CHANGE','WORKFLOW_DEF_CREATE','WORKFLOW_DEF_ALL'])" label="操作" width="380px" align="center">
        <template slot-scope="scope">
          <el-button size="mini" @click="onModleDesign(scope.row.modelId)">设计</el-button>
          <el-button :loading="delLoading" type="primary" size="mini" @click="importModel(scope.row)">导入</el-button>
          <el-popover
            :ref="scope.row.id + 'deploy'"
            placement="top"
            width="180">
            <p>确定要部署吗？</p>
            <div style="text-align: right; margin: 0">
              <el-button size="mini" type="text" @click="$refs[scope.row.id + 'deploy'].doClose()">取消</el-button>
              <el-button :loading="delLoading" type="primary" size="mini" @click="deployModel(scope.row.id,scope.row.modelId)">确定</el-button>
            </div>
            <el-button slot="reference" type="danger" size="mini">部署</el-button>
          </el-popover>
          <el-popover
            :ref="scope.row.id + 'export'"
            placement="top"
            width="180">
            <p>确定要导出吗？</p>
            <div style="text-align: right; margin: 0">
              <el-button size="mini" type="text" @click="$refs[scope.row.id + 'export'].doClose()">取消</el-button>
              <el-button :loading="delLoading" type="primary" size="mini" @click="exportModel(scope.row.id, scope.row.modelId, scope.row.flowKey)">确定</el-button>
            </div>
            <el-button slot="reference" type="danger" size="mini">导出</el-button>
          </el-popover>
          <el-popover
            v-if="scope.row.flowState.toString() === '1'"
            :ref="scope.row.id"
            placement="top"
            width="180">
            <p>确定要挂起吗？</p>
            <div style="text-align: right; margin: 0">
              <el-button size="mini" type="text" @click="$refs[scope.row.id].doClose()">取消</el-button>
              <el-button :loading="delLoading" type="primary" size="mini" @click="suspendFlow(scope.row.id)">确定</el-button>
            </div>
            <el-button slot="reference" type="danger" size="mini">挂起</el-button>
          </el-popover>
          <el-popover
            v-if="scope.row.flowState.toString() === '2'"
            :ref="scope.row.id"
            placement="top"
            width="180">
            <p>确定要激活吗？</p>
            <div style="text-align: right; margin: 0">
              <el-button size="mini" type="text" @click="$refs[scope.row.id].doClose()">取消</el-button>
              <el-button :loading="delLoading" type="primary" size="mini" @click="activeFlow(scope.row.id)">确定</el-button>
            </div>
            <el-button slot="reference" type="primary" size="mini">激活</el-button>
          </el-popover>
        </template>
      </el-table-column>
    </DataGridTable>
    <flowImageSet ref="flowImageSet" :sup_this="sup_this" :click_enable="false" />
    <flowModelDesign ref="flowModelDesign" :sup_this="sup_this" />
    <importBpmnForm ref="importBpmnForm" :sup_this="sup_this" />
  </div>
</template>

<script>
import checkPermission from '@/utils/permission'
import initData from '@/mixins/initData'
import { suspendFlow, activeFlow, loadDataUrl, deployModel, exportModel } from '@/api/system/workflowDef'
import { parseTime } from '@/utils/index'
import eHeader from './module/header'
import eForm from './module/form'
import flowImageSet from './module/flowImageSet'
import flowModelDesign from './module/flowModelDesign'
import importBpmnForm from './module/importBpmnForm'
import ElImageViewer from 'element-ui/packages/image/src/image-viewer'
import { getToken } from '@/utils/auth'

export default {
  components: { importBpmnForm, ElImageViewer, eHeader, flowImageSet, flowModelDesign, eForm },
  mixins: [initData],
  dictTypes: ['flow_state'],
  data() {
    return {
      delLoading: false,
      sup_this: this,
      showViewer: false,
      showImageSet: false,
      flowImageUrlList: []
    }
  },
  created() {
    this.url = loadDataUrl()
    this.searchToggle = true
    this.$nextTick(() => {
      this.init()
    })
  },
  methods: {
    parseTime,
    checkPermission,
    async beforeInit() {
      return true
    },
    onModleDesign(modelId) {
      window.open('static/workflowdesign/modeler.html?modelId=' + modelId + '&X-AUTH-TOKEN=' + getToken(), '_blank')
      /* const _this = this.$refs.flowModelDesign
      _this.modelId = modelId
      _this.initDisplayInfo(modelId)
      _this.dialog = true*/
    },
    onPreview(flowKey) {
      const _this = this.$refs.flowImageSet
      _this.flowKey = flowKey
      _this.initDisplayInfo(flowKey, '')
      _this.dialog = true
    },
    closeViewer() {
      this.showViewer = false
    },
    importModel(row) {
      const _this = this.$refs.importBpmnForm
      _this.initLoadComponent(row)
      _this.dialog = true
    },
    exportModel(id, modelId, flowkey) {
      this.delLoading = true
      // 开一个新窗口
      // this.newWin = window.open()
      exportModel(modelId, flowkey + '.bpmn').then(blob => {
        this.delLoading = false
        this.$refs[id + 'export'].doClose()
        this.$notify({
          title: '导出成功',
          type: 'success',
          duration: 2500
        })
      }).catch(err => {
        this.delLoading = false
        this.$refs[id + 'export'].doClose()
        console.log(err)
      })
    },
    deployModel(id, modelId) {
      this.delLoading = true
      const data = { paramBean: { modelId: modelId, id: id }}
      deployModel(data).then(res => {
        this.delLoading = false
        this.$refs[id + 'deploy'].doClose()
        this.init()
      }).catch(err => {
        this.delLoading = false
        this.$refs[id + 'deploy'].doClose()
        console.log(err)
      })
    },
    suspendFlow(id) {
      this.delLoading = true
      const data = { idList: [id] }
      suspendFlow(data).then(res => {
        this.delLoading = false
        this.$refs[id].doClose()
        this.init()
      }).catch(err => {
        this.delLoading = false
        this.$refs[id].doClose()
        console.log(err)
      })
    },
    activeFlow(id) {
      this.delLoading = true
      const data = { idList: [id] }
      activeFlow(data).then(res => {
        this.delLoading = false
        this.$refs[id].doClose()
        this.init()
      }).catch(err => {
        this.delLoading = false
        this.$refs[id].doClose()
        console.log(err)
      })
    }
  }
}
</script>

<style scoped>

</style>
